/**
 * @Author   adeng.y
 * @DateTime 2019-07-10
 * @description  经销商
 */
$(function(){

	// 关闭弹出层
	$('.J_modShade').on('click',function(){
		var _self = this;
		$(_self).addClass('hide');
		$('.J_pupopCity,.J_modPupopBrand,.J_pupopCarSeries,.J_pupopCarStyle,.J_pupopListCarSeries').removeClass('show');
		$('body').removeClass('ov');
	});
	// 关闭品牌弹窗
	$('.J_iconBack').on('click',function(){
		var $pupbox = $(this).parents('.mod-popup');
		$pupbox.removeClass('show');
		$('body').removeClass('ov');
	});

	/*
		经销商列表
	*/
	if($('.dealer-screen').length > 0){
		// 选择城市
		$('.J_dealerProvince').on('click',function(){
			$('.J_modShade').removeClass('hide');
			$('.J_pupopCity').addClass('show');
			$('body').addClass('ov');
			$.ajax({
				url: '/api/get-province/',
				type: 'GET',
				dataType: 'json',
				beforeSend:function(){
					DSJ.loading();
				},
				success: function(data){
					if (data.code === 0) {
						DSJ.loading.colse();
						var _html = template('J_templateProvince', data);
						$('.J_pupopCity').find('.province').html(_html);
					}else{
						DSJ.alter(data.msg);
					}
				}
			});
		});
		$(document).on('click', '.J_pupopCity .province a', function() {
			var _self = this;
			var _id = $(_self).attr('data-id');
			var _text = $(_self).text();
			$('.J_pupopCity .province a').removeClass('active');
			$(_self).addClass('active');
			$('.J_dealerProvince').addClass('active').text(_text).attr('data-id',_id);
			$.ajax({
				url: '/api/get-city-by-province/',
				type: 'GET',
				dataType: 'json',
				data:{
					p_id:_id
				},
				beforeSend:function(){
					DSJ.loading();
				},
				success: function(data){
					data.pname = _text;
					if (data.code === 0) {
						DSJ.loading.colse();
						var _html = template('J_templateCity', data);
						$('.J_pupopCity').find('.city').html(_html);
					}else{
						DSJ.alter(data.msg);
					}
				}
			});
		});
		$(document).on('click', '.J_pupopCity .city a', function() {
			var _self = this;
			var _id = $(_self).attr('data-id');
			var _aid = $('.J_dealerProvince').attr('data-id');
			$('.J_dealerCity').addClass('active').text($(_self).text()).attr('data-id',_id);
			$('.J_modShade').click();
			// /品牌ID/厂商ID/车系ID/a城市ID/b区域/
			window.location.href='/m/'+$('.J_dealerListBrand').attr('data-bid')+'/0/'+$('.J_dealerListCarSeries').attr('data-sid')+'/a'+$('.J_dealerCity').attr('data-id')+'/';
		});
		$('.J_dealerCity').on('click',function(){
			$('.J_dealerProvince').click();
		});

		// 列表选择品牌
		$('.J_dealerListBrand').on('click',function(){
			$('.J_modPupopListBrand').addClass('show');
			$('body').addClass('ov');
			$.ajax({
				url: '/api/get-brand/',
				type: 'GET',
				dataType: 'json',
				beforeSend:function(){
					DSJ.loading();
				},
				success: function(data){
					if (data.code === 0) {
						DSJ.loading.colse();
						var _html = template('J_templateListBrand', data);
						$('.J_modPupopListBrand').find('.brand-list dl').html(_html);
					}else{
						DSJ.alter(data.msg);
					}
				}
			});
		});
		$(document).on('click', '.J_modPupopListBrand dd a', function() {
			var _self = this;
			var _bid = $(_self).attr('data-bid');
			var $pupbox = $(this).parents('.mod-popup');
			$pupbox.removeClass('show');
			$('body').removeClass('ov');
			$('.J_dealerListBrand').addClass('active').text($(_self).text()).attr('data-bid',_bid);
			window.location.href='/m/'+$('.J_dealerListBrand').attr('data-bid')+'/0/0/a'+$('.J_dealerCity').attr('data-id')+'/';
		});
		// 列表选择车系
		$('.J_dealerListCarSeries').on('click',function(){
			var _bid = $('.J_dealerListBrand').attr('data-bid');
			if( _bid === '0'){
				layer.open({content: '请选择品牌',skin: 'msg',anim: 'scale',time: 2});
				$('.J_dealerListBrand').click();
				return;
			}
			$('.J_modShade').removeClass('hide');
			$('.J_pupopListCarSeries').addClass('show');
			$('body').addClass('ov');
			$.ajax({
				url: '/api/get-factory-by-brand/',
				type: 'GET',
				dataType: 'json',
				data:{brand_id:_bid},
				beforeSend:function(){
					DSJ.loading();
				},
				success: function(data){
					if (data.code === 0) {
						DSJ.loading.colse();
						var _html = template('J_templateListCarSeries', data);
						$('.J_pupopListCarSeries').find('.car-series dl').html(_html);
					}else{
						layer.open({content: data.msg,skin: 'msg',anim: 'scale',time: 2});
					}
				}
			});
		});
		$(document).on('click', '.J_pupopListCarSeries dd a', function() {
			var _self = this;
			var _text = $(_self).find('h5').text();
			var _sid = $(_self).attr('data-sid');
			$('.J_dealerListCarSeries').addClass('active').text(_text).attr('data-sid',_sid);
			window.location.href='/m/'+$('.J_dealerListBrand').attr('data-bid')+'/0/'+$('.J_dealerListCarSeries').attr('data-sid')+'/a'+$('.J_dealerCity').attr('data-id')+'/';
			$('.J_modShade').click();
		});

		inquiryPrice('.J_inquiryPricr');
	}

	/*
		经销商首页
	*/
	if($('.dealer-tab').length > 0){
		var _wind = window;
		var _tabTop = $('.dealer-tab').offset().top;
		$(_wind).scroll(function() {
			var _st = $(_wind).scrollTop();
			if(_st > _tabTop){
				$('.dealer-tab').find('ul').addClass('fix');
			}else{
				$('.dealer-tab').find('ul').removeClass('fix');
			}
		});
		inquiryPrice('.J_inquiryPricr');
	}

	if($('.mod-car-detail').length > 0){
	 var swiper = new Swiper('.swiper-container', {
	 		autoplay:true,
      loop: true,
      pagination: {
        el: '.swiper-pagination',
      }
    });
	}


	// 询底价
	function inquiryPrice(obj){
		function carAjax (obj,_did,_aid,_cid,callback){
			$.ajax({
				url: '/api/get-price-auto/',
				dataType: 'json',
				cache: true,
				data: {dealer_id: _did},
				beforeSend:function(){
					DSJ.loading();
				},
				success: function(data){
					if (data.code === 0) {
						DSJ.loading.colse();
						var _html = template('J_templateSeries', data);
						$('.J_a_series').find('.drop_down_list').html('').append(_html);
            setTimeout(function() {
              typeof(callback) == 'function' && callback();
            }, 300);
					}else{
						DSJ.alter(data.msg);
					}
				}
			});
			if(_aid){
				$.ajax({
					url: '/api/get-price-cx-by-auto/',
					dataType: 'json',
					cache: true,
					data: {dealer_id: _did,auto_id: _aid},
					beforeSend:function(){
						DSJ.loading();
					},
					success: function(data){
						if (data.code === 0) {
							DSJ.loading.colse();
							var _html = template('J_templateStyle', data);
							$('.J_a_style').find('.drop_down_list').html('').append(_html);
							$('.J_pupopCarStyle').find('.car-style dl').html($('.J_a_style').find('.drop_down_list').html());
						}else{
							DSJ.alter(data.msg);
						}
					}
				});
			}
		}

		function loads(){
			var aid = $('.J_a_series').find('.J_inquiryCarSeries').attr('data-aid') || '';
			var cid = $('.J_a_style').find('.J_inquiryCarStyle').attr('data-cid') || '';
			// 由于是异步加载，有可能会模块还没加载进去，所以使用定时器来处理
			setTimeout(function() {
				if(aid){
					$('.J_a_series').find('.drop_down_list a').each(function() {
						var _t = this;
						if(aid == $(_t).attr('data-aid')){
							$('.J_inquiryCarSeries').addClass('active').text($(this).find('h5').text());
						}
					});
				}
				if(cid){
					$('.J_a_style').find('.drop_down_list a').each(function() {
						var _t = this;
						if(cid == $(_t).attr('data-cid')){
							$('.J_inquiryCarStyle').addClass('active').text($(this).text());
						}
					});
				}
			}, 300);
		}

		$(obj).on('click',function(){
			var _did = $(this).attr('data-did');
			var _aid = $(this).attr('data-aid') || '';
			var _cid = $(this).attr('data-cid') || '';
			
			$('.J_modPopupInquiry').addClass('show');
			$('body').addClass('ov');
			$('.mod-popupInquiry').find('input[name="dealer_id"]').val(_did);
			$('.mod-popupInquiry').find('input[name="auto_id"]').val(_aid);
			$('.mod-popupInquiry').find('input[name="cx_id"]').val(_cid);
			$('.J_inquiryCarSeries').attr('data-aid',_aid);
			$('.J_inquiryCarStyle').attr('data-cid',_cid);
			
      carAjax('.J_inquiryCarStyle', _did, _aid, _cid, loads());
		});

		//选择车系
		$(document).on('click', '.J_inquiryCarSeries', function() {
			var _did = $('.mod-popupInquiry').find('input[name="dealer_id"]').val();
			var _aid = $(this).attr('data-aid') || '';
			$('.J_modShade').removeClass('hide');
			$('.J_pupopCarSeries').addClass('show');
			$('body').addClass('ov');

			$('.J_pupopCarSeries').find('.car-series dl').html($('.J_a_series').find('.drop_down_list').html());
		});
		$(document).on('click','.J_pupopCarSeries .car-series dd a', function(){
			var _aid = $(this).attr('data-aid');
			var _text = $(this).find('h5').text();
			$('.J_modShade').addClass('hide');
			$('.J_pupopCarSeries').removeClass('show');
			$('body').removeClass('ov');

			$('.J_inquiryCarSeries').addClass('active').attr('data-aid',_aid).text(_text);
			$('.mod-popupInquiry').find('input[name="auto_id"]').val(_aid);
		});
		$(document).on('click','.J_inquiryCarStyle',function(){
			var _did = $('.mod-popupInquiry').find('input[name="dealer_id"]').val();
			var _aid = $('.J_inquiryCarSeries').attr('data-aid');
			$('.J_modShade').removeClass('hide');
			$('.J_pupopCarStyle').addClass('show');
			$('body').addClass('ov');
			
			$('.J_pupopCarStyle').find('.car-style dl').html($('.J_a_style').find('.drop_down_list').html());
		});
		$(document).on('click','.J_pupopCarStyle .car-style dd a', function(){
			var _text = $(this).text();
			$('.J_inquiryCarStyle').addClass('active').text(_text);
			$('.J_modShade').addClass('hide');
			$('.J_pupopCarStyle').removeClass('show');
			$('body').removeClass('ov');

			$('.mod-popupInquiry').find('input[name="cx_id"]').val($(this).attr('data-sid'));
		});
		$(document).on('submit', '#tuangouForm', function(){
			var self = this;
			var _dealerId = $(this).find('input[name="dealer_id"]').val();
			var _autoId = $(this).find('input[name="auto_id"]').val();
			var _cxId = $(this).find('input[name="cx_id"]').val();
			var _name = $(this).find('input[name="name"]').val();
			var _mobile = $(this).find('input[name="mobile"]').val();
			var _remark = $(this).find('textarea[name="remark"]').val();
			var csfr_param = $('meta[name="csrf-param"]').attr('content');
			var csfr_token = $('meta[name="csrf-token"]').attr('content');
			var _data ={};

			if(!_autoId){
				DSJ.alter('请选择车系');
				return false;
			}
			if(!_name){
				DSJ.alter('请输入您的称呼');
				return false;
			}
			if(!_mobile){
				DSJ.alter('手机号不能为空');
				return false;
			}
			if (!(_mobile && /^1[3456789]\d{9}$/.test(_mobile))) {
				DSJ.alter('请输入正确手机号');
				return false;
			}
			_data = {
				dealer_id: _dealerId,
				auto_id: _autoId,
				cx_id: _cxId,
				name: _name,
				mobile: _mobile,
				remark: _remark
			}
			if(csfr_param) {
				_data[csfr_param] = csfr_token;
			}
			$(this).find('input[type="submit"]').addClass('disabled').attr('disabled','disabled'); //防止重复点击
			DSJ.loading();
			// debugger;
			$.ajax({
				url: $(this).attr("action"),
				type: $(this).attr("method"),
				data: _data,
				success: function(ret){
					if(ret.code === 0){
						DSJ.loading.colse();
						DSJ.alter(ret.msg);
						setInterval(function(){
							window.location.href='/m/';
						}, 1000);
					}else{
						DSJ.alter(ret.msg);
					}
				}
			})
			.always(function() {
				$(self).find('input[name="name"]').val('');
				$(self).find('input[name="mobile"]').val('');
				$(self).find('textarea[name="remark"]').val('');
				$(self).find('input[type="submit"]').removeClass('disabled').removeAttr('disabled');
			});
			return false;
		});
	}
});
